package org.video.mine.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import org.video.mine.R;
import org.video.mine.databinding.HomeTitleBinding;
import org.video.mine.databinding.MovieInfoBinding;
import org.video.mine.databinding.MovieInfoMainBinding;
import org.video.mine.databinding.MovieInfoNameBinding;
import org.video.mine.entity.HomeTitleEntity;
import org.video.mine.entity.MovieInfoEntity;
import org.video.mine.model.ClickModel;

import java.util.List;

public class MovieInfoAdapter extends RecyclerView.Adapter<MovieInfoAdapter.ViewHolder> {

    List<MovieInfoEntity> titleEntities;
    Context context;
    String viewType;

    public MovieInfoAdapter(List<MovieInfoEntity> titleEntities, Context context, String viewType) {
        this.titleEntities = titleEntities;
        this.context = context;
        this.viewType = viewType;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding binding;
        if (viewType==1) {
            binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.movie_info, parent, false);
        }else {
            binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.movie_info_name, parent, false);
        }
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (viewType.equals("first")) {
            MovieInfoEntity titleEntity = titleEntities.get(position);
            if (TextUtils.isEmpty(titleEntity.getImgUrl())) {
//            ((MovieInfoNameBinding)holder.getTitleBinding()).tvMovieName.setText(titleEntity.getMovieType());
                ((MovieInfoNameBinding) holder.getTitleBinding()).setName(titleEntity.getMovieType());
                ((MovieInfoNameBinding) holder.getTitleBinding()).setClick(new ClickModel());
                if (titleEntity.getMovieType().equals("今日热播影片")){
                    ((MovieInfoNameBinding) holder.getTitleBinding()).llMore.setVisibility(View.GONE);
                }else {
                    ((MovieInfoNameBinding) holder.getTitleBinding()).llMore.setVisibility(View.VISIBLE);
                }
            } else {
                ((MovieInfoBinding) holder.getTitleBinding()).setMovieInfo(titleEntity);
                ((MovieInfoBinding) holder.getTitleBinding()).setOnClick(new ClickModel());
                ((MovieInfoBinding) holder.getTitleBinding()).setContext(context);
            }
        }else if (viewType.equals("second")){
            MovieInfoEntity titleEntity = titleEntities.get(position);
            if (TextUtils.isEmpty(titleEntity.getImgUrl())){
                ((MovieInfoNameBinding) holder.getTitleBinding()).setName(titleEntity.getMovieType());
                ((MovieInfoNameBinding) holder.getTitleBinding()).llMore.setVisibility(View.GONE);
            }else {
                ((MovieInfoBinding) holder.getTitleBinding()).setMovieInfo(titleEntity);
                ((MovieInfoBinding) holder.getTitleBinding()).setOnClick(new ClickModel());
                ((MovieInfoBinding) holder.getTitleBinding()).setContext(context);
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (viewType.equals("first")) {
            if (TextUtils.isEmpty(titleEntities.get(position).getImgUrl())) {
                return 0;
            }
            return 1;
        }else if (viewType.equals("second")){
            MovieInfoEntity titleEntity = titleEntities.get(position);
            if (TextUtils.isEmpty(titleEntity.getImgUrl())){
                return 0;
            }else {
                return 1;
            }
        }
        return 0;
    }

    @Override
    public int getItemCount() {
        return titleEntities.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        ViewDataBinding titleBinding = null;

        public ViewHolder(ViewDataBinding titleBinding) {
            super(titleBinding.getRoot());
            this.titleBinding = titleBinding;
        }

        public ViewDataBinding getTitleBinding(){
            return titleBinding;
        }
    }
}
